//
//  XQMainTabBarController.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/3.
//

import UIKit

class XQMainTabBarController: UITabBarController {

    private let customTabBar = UIView()
    
    private var tabBarItems: [XQTabItem] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        setupCustomTabBar()
        setupViewControllers()
        updateTabBarSelection()
        
        if #available(iOS 15.0, *) {
            UITableView.appearance().sectionHeaderTopPadding = 0
            UITableView.appearance().isPrefetchingEnabled = false
        }
    }
    
    public func goHomeController() {
        switchToViewController(at: 0)
    }
    
    public func goMineController() {
        switchToViewController(at: 2)
    }

    private func setupCustomTabBar() {
        tabBar.isHidden = true

        view.addSubview(customTabBar)
        customTabBar.backgroundColor = .white
        
        customTabBar.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(48 + UIDevice.xq_bottomSafeMargin())
        }

        // 添加自定义的 Tab Bar 项
        let itemTitles = ["Home", "Order", "Me"]
        let itemImages = ["tab_home", "tab_order", "tab_me"]
        
        var preItem: XQTabItem?
        for (index, title) in itemTitles.enumerated() {
            let tabBarItem = XQTabItem(title: title, image: UIImage(named: itemImages[index]), selectedImage: UIImage(named: "\(itemImages[index])_select"))
            tabBarItem.tag = index
            tabBarItem.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tabBarItemTapped(_:))))
            customTabBar.addSubview(tabBarItem)
            tabBarItems.append(tabBarItem)
            
            tabBarItem.snp.makeConstraints { make in
                make.top.bottom.equalToSuperview()
                make.width.equalTo(view).multipliedBy(1.0 / CGFloat(itemTitles.count))
                if index == 0 {
                    make.left.equalToSuperview()
                }else if index == itemTitles.count - 1 {
                    make.right.equalToSuperview()
                }else {
                    make.left.equalTo(preItem!.snp.right)
                }
            }
            preItem = tabBarItem
        }
    }

    private func setupViewControllers() {
        let homeVC = XQHomeController()
        let orderVC = XQOrderController()
        let meVC = XQMeController()
        viewControllers = [homeVC, orderVC, meVC]
    }

    @objc private func tabBarItemTapped(_ sender: UITapGestureRecognizer) {
        
        if (!XQInitManager.shared.isLogin()) {
            XQRouteTool.jumpLogin()
            return
        }
        
        guard let item = sender.view as? XQTabItem else { return }
        selectedIndex = item.tag
        switchToViewController(at: selectedIndex)
    }

    private func switchToViewController(at index: Int) {
        self.selectedIndex = index
        updateTabBarSelection()
    }

    private func updateTabBarSelection() {
        for (index, item) in tabBarItems.enumerated() {
            item.isSelected = index == selectedIndex
        }
    }
}
